﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Freshome.m.vws
{
    /// <summary>
    /// Majoraws.xaml 的交互逻辑
    /// </summary>
    public partial class Majoraws : UserControl
    {
        public Majoraws()
        {
            InitializeComponent();
            this.elt.Expanded += new RoutedEventHandler(elt_Expanded);
            this.elb.Expanded += new RoutedEventHandler(elb_Expanded);
            this.ert.Expanded += new RoutedEventHandler(ert_Expanded);
            this.erb.Expanded += new RoutedEventHandler(erb_Expanded);
            this.elm.Expanded += new RoutedEventHandler(elm_Expanded);
            this.erm.Expanded += new RoutedEventHandler(erm_Expanded);
            this.ela.Expanded += new RoutedEventHandler(ela_Expanded);
            this.era.Expanded += new RoutedEventHandler(era_Expanded);
            this.elnut.Expanded += new RoutedEventHandler(elnut_Expanded);
            this.eroom.Expanded += new RoutedEventHandler(eroom_Expanded);
        }

        void eroom_Expanded(object sender, RoutedEventArgs e)
        {
            era.IsExpanded = false;
            erm.IsExpanded = false;
            ert.IsExpanded = false;
            erb.IsExpanded = false;
        }

        void elnut_Expanded(object sender, RoutedEventArgs e)
        {
            ela.IsExpanded = false;
            elb.IsExpanded = false;
            elm.IsExpanded = false;
            elt.IsExpanded = false;
        }
        void era_Expanded(object sender, RoutedEventArgs e)
        {
            eroom.IsExpanded = false;
            erm.IsExpanded = false;
            ert.IsExpanded = false;
            erb.IsExpanded = false;
        }
        void ela_Expanded(object sender, RoutedEventArgs e)
        {
            elnut.IsExpanded = false;
            elb.IsExpanded = false;
            elm.IsExpanded = false;
            elt.IsExpanded = false;
        }
        void erm_Expanded(object sender, RoutedEventArgs e)
        {
            eroom.IsExpanded = false;
            era.IsExpanded = false;
            ert.IsExpanded = false;
            erb.IsExpanded = false;
        }
        void elm_Expanded(object sender, RoutedEventArgs e)
        {
            elnut.IsExpanded = false;
            ela.IsExpanded = false;
            elt.IsExpanded = false;
            elb.IsExpanded = false;
        }
        void erb_Expanded(object sender, RoutedEventArgs e)
        {
            eroom.IsExpanded = false;
            era.IsExpanded = false;
            erm.IsExpanded = false;
            ert.IsExpanded = false;
        }
        void ert_Expanded(object sender, RoutedEventArgs e)
        {
            eroom.IsExpanded = false;
            era.IsExpanded = false;
            erm.IsExpanded = false;
            erb.IsExpanded = false;
        }
        void elt_Expanded(object sender, RoutedEventArgs e)
        {
            elnut.IsExpanded = false;
            ela.IsExpanded = false;
            elm.IsExpanded = false;
            elb.IsExpanded = false;
        }
        void elb_Expanded(object sender, RoutedEventArgs e)
        {
            elnut.IsExpanded = false;
            ela.IsExpanded = false;
            elm.IsExpanded = false;
            elt.IsExpanded = false;
        }

        private const string _ep = "wsHttp_IMultint";
        private void Majoraw_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            var dg = sender as DataGrid;
            if (dg != null)
            {
                if (dg.Name == "dgmeat") e.CanExecute = dgmeat.SelectedItem != null;
                else if (dg.Name == "dgrain") e.CanExecute = dgrain.SelectedItem != null;
                else if (dg.Name == "dgfruit") e.CanExecute = dgfruit.SelectedItem != null;
                else if (dg.Name == "dgwater") e.CanExecute = dgwater.SelectedItem != null;
                else if (dg.Name == "dgegg") e.CanExecute = dgegg.SelectedItem != null;
                else if (dg.Name == "dgbean") e.CanExecute = dgbean.SelectedItem != null;
                else if (dg.Name == "dgmilk") e.CanExecute = dgmilk.SelectedItem != null;
                else if (dg.Name == "dgnut") e.CanExecute = dgnut.SelectedItem != null;
                else if (dg.Name == "dgmushroom") e.CanExecute = dgmushroom.SelectedItem != null;
                else e.CanExecute = dgreen.SelectedItem != null;
            }
            else e.CanExecute = false;
        }
        // 删除主料
        private void Delete_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var dg = sender as DataGrid;
            var vm = this.DataContext as vms.Majoraws;
            if (dg != null && vm != null)
            {
                if (dg.Name == "dgmeat")
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.MeatMaterial>, dgmeat.SelectedItem as mds.MeatMaterial, vm, "肉类类");
                }
                else if (dg.Name == "dgrain")
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.GrainMaterial>, dgrain.SelectedItem as mds.GrainMaterial, vm, "米面类");
                }
                else if (dg.Name == "dgfruit")
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.FruitMaterial>, dgfruit.SelectedItem as mds.FruitMaterial, vm, "水果类");
                }
                else if (dg.Name == "dgwater")
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.WaterMaterial>, dgwater.SelectedItem as mds.WaterMaterial, vm, "水产类");
                }
                else if (dg.Name == "dgegg")
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.EggMaterial>, dgegg.SelectedItem as mds.EggMaterial, vm, "禽蛋类");
                }
                else if (dg.Name == "dgbean")
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.BeanMaterial>, dgbean.SelectedItem as mds.BeanMaterial, vm, "豆制品");
                }
                else if (dg.Name == "dgmilk")
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.MilkMaterial>, dgmilk.SelectedItem as mds.MilkMaterial, vm, "奶制品");
                }
                else if (dg.Name == "dgnut")
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.NutMaterial>, dgnut.SelectedItem as mds.NutMaterial, vm, "坚果类");
                }
                else if (dg.Name == "dgmushroom")
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.MushroomMaterial>, dgmushroom.SelectedItem as mds.MushroomMaterial, vm, "菌菇类");
                }
                else
                {
                    Deletentity(dg.ItemsSource as ObservableCollection<mds.GreenMaterial>, dgreen.SelectedItem as mds.GreenMaterial, vm, "蔬菜类");
                }
            }
        }
        private void Deletentity<T>(ObservableCollection<T> mjs, T mj, vms.Majoraws vm, string titlead) where T : mds.Majoraw
        {
            if (mj != null && mjs != null)
            {
                if (mj.Created == false)
                {
                    if (MessageBox.Show(string.Format("{0}({1})的数据将被删除，继续吗？", titlead, mj.Name), titlead,
                        MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        vm.Helproxy.Syncall(cln =>
                        {
                            if (cln.DeleMajoraw(mj.Raw.Mid)) mjs.Remove(mj);
                        }, new Rmt.MultintClient(_ep));
                    }
                }
                else mjs.Remove(mj);
            }
        }
        // 查找并处理搭配信息（Pairaw）
        private void Find_CanExecuted(object sender, CanExecuteRoutedEventArgs e)
        {
            var dg = sender as DataGrid;
            var vm = this.DataContext as vms.Majoraws;
            if (dg != null && vm != null)
            {
                var mr = dg.SelectedItem as mds.Majoraw;
                if (mr != null)
                {
                    e.CanExecute = mr.IsValid && mr.Raw.Mid > default(short) && vm.Enabled;
                }
                else e.CanExecute = false;
            }
            else e.CanExecute = false;
        }
        private void Find_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var vm = this.DataContext as vms.Majoraws;
            var dg = sender as DataGrid;
            if (dg != null && vm != null)
            {
                var mr = dg.SelectedItem as mds.Majoraw;
                if (mr != null)
                {
                    if (mr.Created || mr.Updated)
                    {
                        MessageBox.Show(string.Format("主料中发现未保存的修改，需要保存后才能继续！"), "保存主料", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                    else
                    {
                        Pairaw dlg = new Pairaw(mr.Raw.Mid, mr.Name, vm.Userole);
                        dlg.Owner = Application.Current.MainWindow;
                        dlg.ShowDialog();
                    }
                }
            }
        }
    }
}
